#include "parameter.h"
#include "ui_parameter.h"
#include <QMessageBox>
#include <QRegExp>
#include <QChart>
#include <QtCharts>
#include "mainwindow.h"

Parameter::Parameter(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Parameter)
{
    ui->setupUi(this);
//    this->setWindowModality(Qt::ApplicationModal);
}

Parameter::~Parameter()
{
    delete ui;
}

//slot1
void Parameter::on_pushButtonConfirm_clicked()
{
    bool set_ok = set_para();
    if(set_ok)
    {
        QMessageBox::information(this,"信息","参数修改完成");
        return;
    }
    QMessageBox::critical(this,"错误","ID输入错误");
}

//functions
bool Parameter::set_para(){
    equ_id = ui->lineEditEquipmentID->text();
    dev_id = ui->lineEditDeviceID->text();

    acc_code = ui->lineEditAccCode->text();
    mask_code = ui->lineEditMaskCode->text();
    t0 = ui->lineEditTiming0->text();
    t1 = ui->lineEditTiming1->text();
    fil_type = ui->comboBoxFilterType->currentText();
    mode = ui->comboBoxMode->currentText();

    send_format = ui->comboBoxSendFormat->currentText();
    frame_type = ui->comboBoxFrameType->currentText();
    frame_format = ui->comboBoxFrameFormat->currentText();
    frame_id = ui->lineEditFrameID->text();
    send_data = ui->lineEditSendData->text().split(",");
    send_gap = ui->lineEditSendGapTime->text();

    for(int i = 0;i<send_data.size();++i)
        send_data[i] = "0x"+send_data[i];

    return (equ_id.contains(QRegExp("^\\d+$")) && dev_id.contains(QRegExp("^\\d+$")));
}

QString Parameter::trans_gain_to_data(const QString& para) const
{//gain and inter. time to frame data
    QString out;
    if(para=="*1") out="00";
    else if(para=="*2") out="01";
    else if(para=="*4") out="02";
    else if(para=="*8") out="03";
    return out;
}

//slots2
void Parameter::update_ui(int i){
    QChart::ChartTheme theme = static_cast<QChart::ChartTheme>(i);
    QPalette pal = window()->palette();

    QFile qss_file;
    if (theme == QChart::ChartThemeDark) {
        qss_file.setFileName(":/dark.qss");
        pal.setColor(QPalette::Window, QRgb(0x121218));
    } else if (theme == QChart::ChartThemeBlueCerulean) {
        qss_file.setFileName(":/dark.qss");
        pal.setColor(QPalette::Window, QRgb(0x40434a));
    } else if (theme == QChart::ChartThemeBrownSand) {
        qss_file.setFileName(":/origin.qss");
        pal.setColor(QPalette::Window, QRgb(0x9e8965));
    } else if (theme == QChart::ChartThemeBlueNcs) {
        qss_file.setFileName(":/origin.qss");
        pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
    } else {
        qss_file.setFileName(":/origin.qss");
        pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
    }
    qss_file.open(QFile::ReadOnly);
    this->setStyleSheet(qss_file.readAll());
    window()->setPalette(pal);
}

void Parameter::deal_change_para(const QString& frame_id,const QString& bund_rate,const QString& gain,const QString& inter_time,const QString& order_gap)
{
    ui->lineEditFrameID->setText(frame_id);

    QStringList datas;
    datas<<"01"<<trans_gain_to_data(gain)<<trans_gain_to_data(inter_time)<<"00"<<"00"<<"00"<<"00"<<"00";
    ui->lineEditSendData->setText(datas.join(","));

    ui->lineEditSendGapTime->setText(order_gap);

    if(bund_rate=="100"){
        ui->lineEditTiming0->setText("04");
        ui->lineEditTiming1->setText("1C");
    }else if(bund_rate=="200"){
        ui->lineEditTiming0->setText("81");
        ui->lineEditTiming1->setText("FA");
    }else if(bund_rate=="400"){
        ui->lineEditTiming0->setText("80");
        ui->lineEditTiming1->setText("FA");
    }else if(bund_rate=="500"){
        ui->lineEditTiming0->setText("00");
        ui->lineEditTiming1->setText("1C");
    }else if(bund_rate=="800"){
        ui->lineEditTiming0->setText("00");
        ui->lineEditTiming1->setText("16");
    }else if(bund_rate=="1000"){
        ui->lineEditTiming0->setText("00");
        ui->lineEditTiming1->setText("14");
    }

    set_para();
}
